const fs = require("fs");
let tableFile = ``;
let fileName = "cssfont";
fs.readFile("tabledata.txt", "utf-8", (err, data) => {
  dealWithFile(data);
});
const dealWithFile = tableFile => {
  let propPattern = />[a-z|\-]{1,20}<\/a>/g;
  let tdPatter = />[\u4e00-\u9fa5|。&;|a-z]{1,30}<\/td/g;
  let td = Array.from(tableFile.match(tdPatter));
  let prop = Array.from(tableFile.match(propPattern));
  let objArr = [];
  let obj = {};
  for (let i = 0; i < td.length; i++) {
    // console.log(prop[i].substr(1, prop[i].length - 2));
    if (prop[i] !== undefined) {
      let propi = prop[i].substr(1, prop[i].length - 5);
      let tdi = td[i].substr(1, td[i].length - 5);
      objArr.push({
        property: propi,
        details: tdi
      });
    }
  }
  console.log(objArr);
  fs.exists("../json", exists => {
    if (exists) {
      fs.writeFile(`../json/${fileName}.json`, JSON.stringify(objArr), data => {
        console.log("写出成功!");
      });
    } else {
      fs.mkdir("../json", () => {
        console.log("文件夹创建成功");
        fs.writeFile(
          `../json/${fileName}.json`,
          JSON.stringify(objArr),
          data => {
            console.log("写出成功!");
          }
        );
      });
    }
  });
};
